package com.example.demo;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-11-16
 * Time: 15:49
 */
@Controller
@RequestMapping("/ResponseController")
public class ResponseController {
    @RequestMapping("/print1")
    public String print1(){
        return "/Hello.html";
        //Controller去掉ResponseBody就可以返回页面
    }

    @ResponseBody
    @RequestMapping("/print2")
    public String print2() {
        return "hello";
    }

    @ResponseBody
    @RequestMapping("/print3")//静态html
    public String print3() {
        return "<h1>return print3</h1>";
    }

    @ResponseBody
    @RequestMapping("/print4")//返回一个Json对象
    public User print5() {
        User user = new User();
        user.setName("wangwu");
        user.setAge(17);
        user.setGender("male");
        return user;
    }

    @ResponseBody
    @RequestMapping("/print5")//设置状态码
    public User print5(HttpServletResponse response) {
        User user = new User();
        user.setName("wangwu");
        user.setAge(17);
        user.setGender("male");
        response.setStatus(500);
        return user;
    }

    @ResponseBody
    @RequestMapping(value = "/print6",produces = "application/json")//设置header
    public String print6() {
        return "{\"success\":true}";
    }

    @ResponseBody
    @RequestMapping("/print7")
    public String print7(HttpServletResponse response) {
        response.setHeader("Mykey","Myvalue");
        return "succee";
    }

}
